/**
 * Copyright © 2018 eqxiu.com 北京中网易企秀科技有限公司  All rights reserved.
 */
package com.eqxiu.iom.msg.api.impl;

import com.eqxiu.iom.msg.api.MsgEventApi;
import com.eqxiu.iom.msg.dto.BehaviorEvent;
import com.eqxiu.iom.msg.service.MessageService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @description 站内信服务
 * @author hanweisong
 * @date 2018/12/6 18:21
 */
@Service(version = "${dubbo.version.iom.msg}")
public class MsgEventApiImpl implements MsgEventApi {

    private Logger LOGGER = LoggerFactory.getLogger(MsgEventApiImpl.class);

    @Autowired
    private MessageService messageService;

    @Override
    public void sendMessage(BehaviorEvent event) {
        LOGGER.debug("request-----------");
        long m = System.currentTimeMillis();
        // 参数必要性校验
        if (event == null) {
            LOGGER.warn("BehaviorEvent must noe be null.");
            return;
        }
        // 参数必要性校验
        if (StringUtils.isEmpty(event.getUserId()) || StringUtils.isEmpty(event.getProducerCode()) || StringUtils.isEmpty(event.getEventCode())) {
            LOGGER.warn("Some parameters must not be null. msg: {}：", event);
            return;
        }

        // 分发到消息中心MQ
        messageService.sendMsgCenterMessage(event);

        // 分发到用户行为触发MQ
        messageService.sendUserBehaviorMessage(event);

        // 总计花费时间
        long total = System.currentTimeMillis() - m;
        LOGGER.info("spend: {} ms.", total);
        if (total > 50) {
            LOGGER.info("Spend: {} ms. Spend time too long. BehaviorEvent：{}", total, event);
        }
    }

}
